const query = require("../db/mysql")
const { success, error } = require("../module/module")


class Order {
    //类型筛选
    orderSelect = async (ctx) => {
        let { value } = ctx.request.body
        let sql = `select * from orderList where 1=1 `
        if (value) {
            sql += `and type like "%${value}%"`
        }
        let data = await query(sql)
        if (data.length) {
            ctx.body = success(data, '查找成功')
        } else {
            ctx.body = error('查找成功')
        }
    }
    //模糊搜素
    searchInp = async (ctx) => {
        let { value } = ctx.request.body
        let sql = `select * from orderList where 1=1 `
        console.log(value)
        if (typeof value === 'string') {
            sql += `and name like "%${value}%" `
        } else if (typeof value === 'number') {
            sql += `and number like "%${value}%" `
        }

        let data = await query(sql)
        ctx.body = success(data, '查找成功')
    }
    //删除
    orderDelete = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from orderList where 1=1 and id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        }
    }
    //获取数据
    getOrder = async (ctx) => {
        let { pageIndex, pageSize } = ctx.query
        let sql = `select * from orderList where 1=1 `
        let countSql = 'select count(id) as total from orderList '
        let total = await query(countSql)
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex * 1 - 1) * pageSize * 1},${pageSize * 1} `
        }
        let data = await query(sql)
        ctx.body = {
            total: total[0].total,
            data,
            code: 1
        }
    }
}

module.exports = new Order()